package club.beimeng.eduservice.controller;


import club.beimeng.commonutils.R;
import club.beimeng.eduservice.entity.EduVideo;
import club.beimeng.eduservice.entity.video.VideoFrom;
import club.beimeng.eduservice.entity.vo.VideoUrlVo;
import club.beimeng.eduservice.client.VodClient;
import club.beimeng.eduservice.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-25
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    // 1 添加课时=小节
    @ApiOperation(value = "新增课时")
    @PostMapping("saveVideoInfo")
    public R save(@ApiParam(name = "videoForm", value = "课时对象", required = true) @RequestBody VideoFrom videoFrom){
        Boolean save = videoService.saveVideoInfo(videoFrom);
        return save ? R.ok() : R.error();
    }

    // 2 根据id查询课时
    @ApiOperation(value = "根据id查询课时")
    @PostMapping("getVideoInfo/{videoId}")
    public R save(@ApiParam(name = "videoId", value = "课时Id", required = true)@PathVariable String videoId){
        EduVideo eduVideo = videoService.getById(videoId);
        return R.ok().data("eduVideo",eduVideo);
    }

    // 3 根据库id修改课时
    @ApiOperation(value = "根据id修改课时")
    @PostMapping("updateVideoInfo")
    public R updateVideoInfo(@ApiParam(name = "videoFrom", value = "含有id的from", required = true)@RequestBody VideoFrom videoFrom){
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoFrom,eduVideo);
        boolean b = videoService.updateById(eduVideo);
        return b ? R.ok() : R.error();
    }

    //4 根据id删除课时
    @ApiOperation(value = "根据id删除课时")
    @DeleteMapping("deleteVideoInfo/{videoId}")
    public R deleteVideoInfo(@ApiParam(name = "videoId", value = "课时ID", required = true)@PathVariable String videoId){
        EduVideo eduVideo = videoService.getById(videoId);
        boolean b = videoService.removeById(videoId);
        if (!eduVideo.getVideoSourceUrl().isEmpty()){
            VideoUrlVo videoUrlVo = new VideoUrlVo();
            videoUrlVo.setVideoUrl(eduVideo.getVideoSourceUrl());
            R a = vodClient.removeVideo(videoUrlVo);
        }

        return b  ? R.ok() : R.error();
    }
}

